/**
 * 
 */
package com.valohyd.android_utils;

import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

/**
 * Affiche un progressDialog jusqu'au chargement complet de la page
 * @attention : set le webChromeClient de la webView 
 * 
 * @author ValohydTeam
 *
 */
public class WebViewClientShowDialog extends WebViewClient {
    private ProgressDialog dialog;
    private Context mContext;
    private WebView mWebView;
    
    public WebViewClientShowDialog(Context c, WebView w) {
        super();
        mContext = c;
        mWebView = w;
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        
        if(dialog != null)
            dialog.dismiss();
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
        
        if(dialog == null || !dialog.isShowing()){
            dialog = EasyShowDialogs.showProgressDialogDeterminated(mContext, "Chargement de la page en cours", "Veuillez patienter");
            mWebView.setWebChromeClient(new WebChromeClientProgress(dialog));
        }
        
    }
    
    private class WebChromeClientProgress extends WebChromeClient{
        private ProgressDialog dialog;
        
        public WebChromeClientProgress(ProgressDialog p) {
            super();
            dialog = p;
        }


        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            super.onProgressChanged(view, newProgress);
            if(dialog != null && dialog.isShowing()){
                dialog.setProgress(newProgress);
            }
        }
        
    }
}
